package com.bw1806.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.bw1806.entity.ResultPage;
import com.bw1806.mapper.*;
import com.bw1806.pojo.*;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.StringUtil;

import javax.annotation.Resource;
import java.util.List;

/**
 * @描述
 * @创建人 wangyue
 * @创建时间2020/11/1310:40
 */
@Service(timeout = 5000,retries = 0)
public class TbBrandServiceImpl implements TbBrandService{

    @Resource
    private TbBrandMapper tbBrandMapper;

    @Resource
    private TTypeMapper tTypeMapper;

    @Resource
    private TAihaoMapper tAihaoMapper;

    @Resource
    private TAihaoBrandMapper tAihaoBrandMapper;

    @Resource
    private TbItemCatMapper tbItemCatMapper;

    @Override
    public ResultPage<TbBrand> list(TbBrand tbBrand, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);

        TbBrandExample example =new TbBrandExample();
        TbBrandExample.Criteria criteria = example.createCriteria();
        if(tbBrand != null){
            if(StringUtil.isNotEmpty(tbBrand.getName())){
                criteria.andNameLike("%"+tbBrand.getName()+"%");
            }
            if(StringUtil.isNotEmpty(tbBrand.getFirstChar())){
                criteria.andFirstCharEqualTo(tbBrand.getFirstChar());
            }
        }

        Page<TbBrand> page= (Page<TbBrand>) tbBrandMapper.selectByExample(example);
        return new ResultPage<TbBrand>(page.getTotal(),page.getResult());
    }

    @Override
    public TbBrand getById(Long id) {
        TbBrand tbBrand = tbBrandMapper.selectByPrimaryKey(id);
        List<Long> ahids = tAihaoBrandMapper.selectByBid(id);
        tbBrand.setAhids(ahids);
        return tbBrand;
    }

    @Override
    public void save(TbBrand tbBrand) {
        Long id = tbBrand.getId();
        if(id == null || id == 0){
            tbBrandMapper.insert(tbBrand);
        } else {
            tbBrandMapper.updateByPrimaryKey(tbBrand);
            //根据bid删除
            TAihaoBrandExample example = new TAihaoBrandExample();
            TAihaoBrandExample.Criteria criteria = example.createCriteria();
            criteria.andBidEqualTo(tbBrand.getId().intValue());
            tAihaoBrandMapper.deleteByExample(example);
        }
        for(Long ahid : tbBrand.getAhids()){
            TAihaoBrand tAihaoBrand = new TAihaoBrand(tbBrand.getId().intValue(),ahid.intValue());
            tAihaoBrandMapper.insert(tAihaoBrand);
        }
    }

    @Override
    public void deletbth(Long[] ids) {
       for(Long id : ids){
            tbBrandMapper.deleteByPrimaryKey(id);
           //根据bid删除
           TAihaoBrandExample example = new TAihaoBrandExample();
           TAihaoBrandExample.Criteria criteria = example.createCriteria();
           criteria.andBidEqualTo(id.intValue());
           tAihaoBrandMapper.deleteByExample(example);
        }
        //数组转list
       /* List<Long> longs = Arrays.asList(ids);
        //根据条件删除  where id in（1,2,3）
        TbBrandExample example =new TbBrandExample();
        TbBrandExample.Criteria criteria = example.createCriteria();
        criteria.andIdIn(longs);
        tbBrandMapper.deleteByExample(example);*/
    }

    @Override
    public List<TType> getTypes() {
        return tTypeMapper.selectByExample(null);
    }

    @Override
    public TType getTTypeById(Integer id) {
        return tTypeMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<TAihaoKey> getTAihaoKeys() {
        return tAihaoMapper.selectByExample(null);
    }

    @Override
    public List<TbItemCat> getTbItemCats(Long pid) {

        TbItemCatExample example = new TbItemCatExample();
        TbItemCatExample.Criteria criteria = example.createCriteria();
        criteria.andParentIdEqualTo(pid);
        return tbItemCatMapper.selectByExample(example);
    }
}
